This project contains Ansible playbooks and configuration files to set up and manage the SonarQube server with PostgreSQL for Cosmo-Tech.
.
├── ansible.cfg # Ansible configuration
├── group_vars/ # Variables shared across playbooks
│ └── all.yml # Common variables for all environments
├── hosts.ini # Inventory file for production
├── inventories/ # Environment-specific inventories
│ └── dev/ # Development environment
│ ├── hosts # Development hosts file
│ └── group_vars/ # Development-specific variables
│ └── all.yml # Only contains overrides from group_vars/all.yml
├── playbooks/ # Playbook directory
│ └── main.yml # Main playbook that includes roles
├── roles/ # Roles directory
│ ├── docker/ # Docker role
│ │ ├── defaults/ # Default variables for Docker
│ │ │ └── main.yml
│ │ └── tasks/ # Tasks for Docker installation
│ │ └── main.yml
│ ├── postgresql/ # PostgreSQL role
│ │ ├── defaults/ # Default variables for PostgreSQL
│ │ │ └── main.yml
│ │ └── tasks/ # Tasks for PostgreSQL installation
│ │ └── main.yml
│ └── sonarqube/ # SonarQube role
│ ├── defaults/ # Default variables for SonarQube
│ │ └── main.yml
│ ├── meta/ # Role metadata
│ │ └── main.yml
│ ├── tasks/ # Tasks for SonarQube installation
│ │ └── main.yml
│ └── templates/ # Templates for SonarQube
│ └── docker-compose.yml.j2 # Docker Compose template
├── tests/ # Test directory
│ ├── integration/ # Integration tests
│ │ └── test_sonarqube.py
│ ├── conftest.py # Pytest configuration
│ ├── run_tests.py # Test runner script
│ └── syntax_check.yml # Syntax and linting checks
└── Vagrantfile # Vagrant configuration for local development
uv venv
source .venv/bin/activate
# Core dependencies
uv pip install .
# Testing dependencies
pip install -r requirements.txt
# Ubuntu/Debian
sudo apt-get update
sudo apt-get install -y virtualbox vagrant docker.io
# Enable Docker
sudo systemctl enable docker
sudo systemctl start docker
sudo usermod -aG docker $USER # Log out and back in after this
export SONARQUBE_DB_PASSWORD="your_secure_password"
Run the Ansible playbook to install PostgreSQL and SonarQube on the production server:
ansible-playbook playbooks/main.yml
vagrant up
ansible-playbook -i inventories/dev/hosts playbooks/main.yml
Alternatively, you can provision during VM creation:
ANSIBLE_PROVISION=true vagrant up
http://localhost:9000
vagrant halt
vagrant destroy
After successful installation, SonarQube will be available at:
http://<server-ip>:9000http://localhost:9000Default credentials:
You can customize the installation by modifying the variables in:
group_vars/all.yml - Contains common variables for all environmentsinventories/dev/group_vars/all.yml - Contains only development-specific overridesroles/*/defaults/main.yml - Contains role-specific settingsThe project has been simplified to reduce duplication and make maintenance easier. Role-specific variables are kept in their respective role defaults to ensure they're always available when the role is executed, while common variables are centralized in group_vars/all.yml.
The local development VM is configured with:
This project includes comprehensive testing mechanisms to ensure proper functionality. You can run all tests using the provided helper script or execute specific tests individually.
Use the simplified test runner script:
# For Vagrant environment
python tests/run_tests.py --vagrant
# For production environment
python tests/run_tests.py --host=<your-server-ip>
# To skip integration tests
python tests/run_tests.py --vagrant --skip-integration
This will run:
# Run syntax checks and linting
ansible-playbook tests/syntax_check.yml
# Run only ansible-lint
ansible-lint
After deployment, verify the SonarQube installation:
# For production
./tests/integration_test.sh <server-ip> 9000
# For local Vagrant VM
./tests/integration_test.sh localhost 9000
Test individual roles in isolation:
# Test PostgreSQL role
cd roles/postgresql
molecule test
# Test SonarQube role
cd roles/sonarqube
molecule test
Verify that running the playbook multiple times doesn't cause changes:
# First run
ansible-playbook -i inventories/dev/hosts playbooks/main.yml
# Second run (should report no changes)
ansible-playbook -i inventories/dev/hosts playbooks/main.yml
.ansible-lint - Ansible linting rules and exclusionsmolecule/*/molecule.yml - Molecule test environment configurationtests/syntax_check.yml - Syntax and linting test definitionstests/integration_test.sh - Integration test scriptpip install molecule molecule-plugins[vagrant] yamllint ansible-lint
# Ubuntu/Debian
sudo apt-get install virtualbox vagrant